package com.gxc.healthfood.controller;

import com.gxc.healthfood.common.ResponseResult;
import com.gxc.healthfood.dto.RegisterDTO;
import com.gxc.healthfood.entity.Users;
import com.gxc.healthfood.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器，处理用户相关的HTTP请求
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UsersService usersService;

    /**
     * 用户登录接口
     * @param loginData 包含用户名和密码的请求体
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseResult<Map<String, Object>> login(@RequestBody Map<String, String> loginData) {
        // 验证请求参数
        if (loginData == null || !loginData.containsKey("username") || !loginData.containsKey("password")) {
            return ResponseResult.error("请求参数不完整");
        }
        
        String username = loginData.get("username");
        String password = loginData.get("password");
        
        // 验证用户名和密码是否为空
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            return ResponseResult.error("用户名或密码不能为空");
        }
        
        // 验证登录信息
        boolean isValid = usersService.verifyLogin(username, password);
        
        if (isValid) {
            // 登录成功
            Users user = usersService.findByUsername(username);
            // 创建一个不包含密码的用户信息对象返回
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            userInfo.put("role", user.getRole());
            
            return ResponseResult.success("登录成功", userInfo);
        } else {
            // 登录失败
            return ResponseResult.error("用户名或密码错误");
        }
    }
    
    /**
     * 用户注册接口
     * @param registerDTO 包含用户名、邮箱和密码的注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public ResponseResult<Map<String, Object>> register(@Validated @RequestBody RegisterDTO registerDTO) {
        // 检查用户名是否已存在
        if (usersService.findByUsername(registerDTO.getUsername()) != null) {
            return ResponseResult.error("用户名已存在");
        }
        
        // 检查邮箱是否已存在
        if (usersService.findByEmail(registerDTO.getEmail()) != null) {
            return ResponseResult.error("邮箱已被注册");
        }
        
        // 执行注册
        boolean success = usersService.registerUser(registerDTO);
        
        if (success) {
            Map<String, Object> data = new HashMap<>();
            data.put("username", registerDTO.getUsername());
            data.put("email", registerDTO.getEmail());
            return ResponseResult.success("注册成功", data);
        } else {
            return ResponseResult.error("注册失败，请稍后重试");
        }
    }
}
